package it.alertManager.util;

import it.alertManager.exceptions.AlertManagerException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;



public class CommonData {
	
	private static final String LOG_CLASS_NAME = CommonData.class.getName();

	public static String genericList[] = {"one","second", "third", "fouth" ,"fifth", "sixth" };
	public static enum periodOfDay {Notte, Mattino, Pomeriggio, Sera};
	public static enum spokenLanguage {it, en, fr, de, zh, ja, ko, zh_CN, zh_TW};

	public static final String LIBERO_WEATHER_FORECAST_URL = "http://meteo.libero.it/ricerca.php?x=0&y=0&cerca=";
	public static final String GOOGLE_WEATHER_FORECAST_URL = "http://www.google.com/ig/api?weather=";

	
	public static final String IT_HOROSCOPE_ULR = "http://www.oroscopi.com/oroscopo/oroscopo_giorno.php?segno=";
	public static final String EN_HOROSCOPE_URL = "http://www.dailyscopes.com/daily/";
	
	public static final float SPEACH_RATE = 0.85f;
	public static final int TOAST_3_SECOND_LENGTH = 5; 
	public static final int ALL_RSS_ITEM = -1;
	
	public static final HashMap<String, String> vowelsTransformation =  new HashMap<String, String>() {{
		put("à","a'");
		put("è","e'");
		put("é","e'");
		put("ì","i'");
		put("ò","o'");
		put("ù","u'");
	}};
	
	public static final Locale getLanguage(String language, Locale defaultLanguage)
	{
		Locale loc = null;
		
		if (spokenLanguage.it.toString().equals(language))
			loc = Locale.ITALIAN;
		else if (spokenLanguage.en.toString().equals(language))
			loc = Locale.ENGLISH;
		else if (spokenLanguage.fr.toString().equals(language))
			loc = Locale.FRENCH;
		else if (spokenLanguage.de.toString().equals(language))
			loc = Locale.GERMAN;
		else if (spokenLanguage.zh.toString().equals(language))
			loc = Locale.CHINESE;	
		else if (spokenLanguage.ja.toString().equals(language))
			loc = Locale.JAPANESE;		
		else if (spokenLanguage.ko.toString().equals(language))
			loc = Locale.KOREAN;				
		else if (spokenLanguage.zh_CN.toString().equals(language))
			loc = Locale.SIMPLIFIED_CHINESE;
		else if (spokenLanguage.zh_TW.toString().equals(language))
			loc = Locale.TRADITIONAL_CHINESE;	
		else if (spokenLanguage.zh_TW.toString().equals(language))
			loc = Locale.TRADITIONAL_CHINESE;	
		
		if (loc == null)
			loc = defaultLanguage;
		
		return loc;
	}
	
	public static final String getResponse(String url) throws AlertManagerException
	{
		HttpGet getMethod = new HttpGet(url);
		DefaultHttpClient client = new DefaultHttpClient();
		HttpResponse response;
        String result = "";
		try {
			response = client.execute(getMethod);
	        InputStream in = response.getEntity().getContent();
	        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
	        StringBuilder str = new StringBuilder();
	        String line = null;
	        while((line = reader.readLine()) != null){
	            str.append(line);
	        }
	        in.close();	
	        result = str.toString();
		} catch (ClientProtocolException e) {
			Log.e(LOG_CLASS_NAME, "Error in CommonData: " + e.getMessage());
			throw new AlertManagerException("Error in CommonData: " + e.getMessage(), e);
		} catch (IOException e) {
			Log.e(LOG_CLASS_NAME, "Error in CommonData: " + e.getMessage());
			throw new AlertManagerException("Error in CommonData: " + e.getMessage(), e);
		}
        return result;
    }	
	
	public static final String vowelsTransformation(String string)
	{
		for (String keyVowel : vowelsTransformation.keySet()) {
			string = string.replaceAll(keyVowel, vowelsTransformation.get(keyVowel));
		}

		return string;
	}
	
}
